// pages/profile/profile.js
Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    isGuest: false,
    menuItems: [
      {
        id: 1,
        title: '我的订单',
        icon: '/images/order.png',
        url: '/pages/order/list/list'
      },
      {
        id: 2,
        title: '优惠券中心',
        icon: '/images/coupon.png',
        url: '/pages/coupon/coupon'
      },
      {
        id: 3,
        title: '收货地址',
        icon: '/images/location.png',
        url: '/pages/address/list/list'
      },
      {
        id: 4,
        title: '我的收藏',
        icon: '/images/收藏.png',
        url: '/pages/favorite/favorite'
      },
      {
        id: 5,
        title: '联系客服',
        icon: '/images/客服.png',
        url: '/pages/customer-service/customer-service'
      },
      {
        id: 6,
        title: '设置',
        icon: '/images/设置.png',
        url: '/pages/setting/setting'
      }
    ]
  },

  onLoad() {
    this.checkUserInfo()
  },

  onShow() {
    this.checkUserInfo()
  },

  // 检查用户信息
  checkUserInfo() {
    const app = getApp();
    const userInfo = wx.getStorageSync('userInfo');
    const isGuest = wx.getStorageSync('isGuest');
    
    if (userInfo) {
      this.setData({
        userInfo,
        hasUserInfo: true,
        isGuest: isGuest || false
      });
    } else {
      this.setData({
        userInfo: null,
        hasUserInfo: false,
        isGuest: false
      });
    }
  },

  // 获取用户信息
  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善用户资料',
      success: (res) => {
        const userInfo = res.userInfo
        this.setData({
          userInfo,
          hasUserInfo: true
        })
        
        // 保存用户信息到本地
        wx.setStorageSync('userInfo', userInfo)
        
        // 调用登录接口
        this.login()
      },
      fail: (err) => {
        console.log('获取用户信息失败', err)
      }
    })
  },

  // 登录
  login() {
    wx.login({
      success: (res) => {
        if (res.code) {
          // 调用后端登录接口
          const app = getApp()
          app.request({
            url: '/api/user/login',
            method: 'POST',
            data: {
              code: res.code,
              nickName: this.data.userInfo.nickName,
              avatarUrl: this.data.userInfo.avatarUrl
            },
            success: (result) => {
              if (result.success) {
                wx.setStorageSync('token', result.data.token)
                wx.showToast({
                  title: '登录成功',
                  icon: 'success'
                })
              }
            },
            fail: (err) => {
              console.log('登录失败', err)
              wx.showToast({
                title: '登录失败',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },

  // 编辑资料
  onEditProfile() {
    wx.navigateTo({
      url: '/pages/profile/edit/edit'
    })
  },

  // 菜单项点击
  onMenuTap(e) {
    const { url } = e.currentTarget.dataset;
    const app = getApp();
    
    // 检查登录状态
    const loginStatus = app.checkLogin(false);
    
    if (!loginStatus) {
      // 未登录，显示登录选择
      wx.showModal({
        title: '提示',
        content: '请先登录或使用游客模式',
        confirmText: '去登录',
        cancelText: '游客模式',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            });
          } else {
            // 进入游客模式
            app.setGuestMode();
            this.checkUserInfo();
            // 游客模式下跳转到页面
            wx.navigateTo({
              url
            });
          }
        }
      });
      return;
    }
    
    // 已登录或游客模式，直接跳转
    wx.navigateTo({
      url
    });
  },

  // 退出登录
  onLogout() {
    const app = getApp();
    const isGuest = this.data.isGuest;
    
    const title = isGuest ? '退出游客模式' : '确认退出';
    const content = isGuest ? '确定要退出游客模式吗？' : '确定要退出登录吗？';
    
    wx.showModal({
      title: title,
      content: content,
      success: (res) => {
        if (res.confirm) {
          // 调用全局退出方法
          app.logout();
          
          // 更新页面状态
          this.setData({
            userInfo: null,
            hasUserInfo: false,
            isGuest: false
          });
        }
      }
    });
  },
  
  // 切换到正式登录
  onSwitchToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  }
})